#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define N 8
int solutions = 0;
bool issafe(int* board, int row, int cow) {
	for (int i = 0;i < row;i++) {
		if (board[i] == cow) {
			return false;
		}
		if (abs(board[i] - cow) == abs(row - i)) {
			return false;
		}
	}
	return true;
}
void solveQueens(int* board, int row,int num) {
	if (row == 8) {
		solutions++;
		if (num == solutions) {
			for (int j = 0;j < N;j++) {
				printf("%d", board[j]);
			}
			printf("\n");
		}
		return;
	}
	if (row < 8) {
		for (int i = 1;i <= 8;i++) {
			board[row] = i;
			if (issafe(board, row, i)) {
				solveQueens(board,row+1,num);
			}
		}
	}
}
int main() {
	int board[N];
	for (int i = 0;i < N;i++) {
		board[i] = -1;
	}
	int n;
	scanf("%d", &n);  
	for (int i = 0;i < n;i++) {
		int num;
		scanf("%d", &num);
		solveQueens(board,0,num);
		solutions = 0;
	}
}
